package blow.sky.quoted.ocr;

import blow.sky.quoted.ocr.api.OcrService;
import org.apache.thrift.protocol.TBinaryProtocol;
import org.apache.thrift.protocol.TProtocol;
import org.apache.thrift.transport.TSocket;
import org.apache.thrift.transport.TTransport;
import org.apache.thrift.transport.layered.TFramedTransport;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;

/**
 * @Description 图像文字识别
 * @Author sun
 * @Date 2021/6/3 22:07
 */

@Component
public class OcrServiceClient
{
    @Value("${thrift.ocr.host}")
    private String ocrServerHost;
    @Value("${thrift.ocr.port}")
    private int ocrServerPort;

    private TTransport transport = null;
    private OcrService.Client client = null;

    public OcrService.Client getOcrService()
    {
        if (client != null && transport != null && transport.isOpen())
            return client;
        try {
            TSocket socket = new TSocket(ocrServerHost, ocrServerPort);
            transport = new TFramedTransport(socket);
            transport.open();
            TProtocol protocol = new TBinaryProtocol(transport);
            client = new OcrService.Client(protocol);
        }
        catch (Exception e) {
            e.printStackTrace();
            client = null;
        }
        return client;
    }
}
